home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / bin / setupcon < prev    next >
Text File  |  2008-08-27  |  9KB  |  331 lines

  1. #!/bin/sh
  2.  
  3. #     setupcon -- setup the font and keyboard on the Linux console
  4. #     Copyright ┬⌐ 1999,2000,2001,2002,2003,2006 Anton Zinoviev
  5.  
  6. #     This program is free software; you can redistribute it and/or modify
  7. #     it under the terms of the GNU General Public License as published by
  8. #     the Free Software Foundation; either version 2 of the License, or
  9. #     (at your option) any later version.
  10.  
  11. #     This program is distributed in the hope that it will be useful,
  12. #     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. #     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. #     GNU General Public License for more details.
  15.  
  16. #     If you have not received a copy of the GNU General Public License
  17. #     along with this program, write to the Free Software Foundation, Inc.,
  18. #     59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  
  20. ###########################################################################
  21.  
  22.  
  23. # The same as /usr/bin/which - in order to make "which" available before
  24. # /usr is mounted
  25. which () {
  26.     local IFS
  27.     IFS=:
  28.     for i in $PATH; do
  29.     if [ -x "$i/$1" ]; then
  30.         echo "$i/$1"
  31.         return 0
  32.     fi
  33.     done
  34.     return 1
  35. }
  36.  
  37. while [ "$1" ]; do
  38.     case "$1" in
  39.     -k|--keyboard-only)
  40.         keyboard_only=yes
  41.         ;;
  42.     -f|--font-only)
  43.         font_only=yes
  44.         ;;
  45.     -v|--verbose)
  46.         verbose_option=yes
  47.         ;;
  48.     --force)
  49.         force=yes
  50.         ;;
  51.     --save)
  52.         save=yes
  53.         ;;
  54.     --save-only)
  55.         force=yes
  56.         save=yes
  57.         save_only=yes
  58.         ;;
  59.     -h|--help)
  60.         cat >&2 <<EOF
  61. Usage: setupcon [OPTION] [VARIANT]
  62. Sets up the font and the keyboard on Linux console.
  63.  
  64.   -k, --keyboard-only  setup the keyboard only, do not setup the font
  65.   -f, --font-only      setup the font only, do not setup the keyboard
  66.       --force          do not check whether we are on the console
  67.   -v, --verbose        explain what is being doing, try it if s.t. goes wrong
  68.       --save           copy the font and the ACM in /etc/console-setup,
  69.                          update /etc/console-setup/boottime.kmap.gz
  70.       --save-only      only save; don't setup keyboard/font immediately
  71.                          (implies --force)
  72.   -h, --help           display this help and exit
  73.  
  74. If VARIANT is not specified setupcon looks for the configuration files
  75. (in this order) ~/.console-setup and /etc/default/console-setup.  When
  76. a VARIANT is specified then setupcon looks for the configuration files
  77. ~/.console-setup.VARIANT and /etc/default/console-setup.VARIANT.
  78. EOF
  79.         exit 0
  80.         ;;
  81.     -*)
  82.         echo "setupcon: Unrecognised option $1" >&2
  83.         exit 1
  84.         ;;
  85.     *)
  86.         if [ -z "$VARIANT" ]; then
  87.         VARIANT="$1"
  88.         else
  89.         echo "setupcon: Two variants specified: $VARIANT and $1" >&2
  90.         exit 1
  91.         fi
  92.         ;;
  93.     esac
  94.     shift
  95. done
  96.  
  97. if [ "$VARIANT" ]; then
  98.     USER_CONFIG=${HOME}/.console-setup."$VARIANT"
  99.     MAIN_CONFIG=/etc/default/console-setup."$VARIANT"
  100. else
  101.     USER_CONFIG=${HOME}/.console-setup
  102.     MAIN_CONFIG=/etc/default/console-setup
  103. fi
  104.  
  105. if [ -f "$USER_CONFIG" ]; then
  106.     CONFIG="$USER_CONFIG"
  107. elif [ -f "$MAIN_CONFIG" ]; then
  108.     CONFIG="$MAIN_CONFIG"
  109. else
  110.     echo "setupcon: None of $MAIN_CONFIG nor $USER_CONFIG exists." >&2
  111.     exit 1
  112. fi
  113.  
  114. . "$CONFIG"
  115.  
  116. if [ -d /lib/debian-installer ]; then
  117.     CHARMAP=UTF-8
  118. fi
  119.  
  120. if [ "$verbose_option" = yes ]; then
  121.     VERBOSE_OUTPUT=yes
  122. fi
  123. if [ "$VERBOSE_OUTPUT" = yes ]; then
  124.     verbose=''
  125. else
  126.     verbose='>/dev/null 2>&1'
  127. fi
  128.  
  129. if which ckbcomp-mini >/dev/null && [ "$CHARMAP" != UTF-8 ]; then
  130.     CHARMAP=UTF-8
  131.     if [ "$VERBOSE_OUTPUT" = yes ]; then
  132.     echo Only UTF-8 is supported by console-setup-mini
  133.     fi
  134. fi
  135.  
  136. if [ "$force" != yes ]; then
  137.     case `readlink /proc/self/fd/2` in
  138.     /dev/tty[0-9]*|/dev/vc/[0-9]*|/dev/console)
  139.         ;;
  140.     *)
  141.         if [ "$VERBOSE_OUTPUT" = yes ]; then
  142.         echo We are not on the Linux console, exiting.
  143.         fi
  144.         exit 0 
  145.         ;;
  146.     esac
  147. fi
  148.  
  149. #-----------------------#
  150. #       OUTPUT          #
  151. #-----------------------#
  152.  
  153. if [ "$keyboard_only" != yes ] && [ "$ACTIVE_CONSOLES" ]; then
  154.     for console in $ACTIVE_CONSOLES; do
  155.     [ -w $console ] || continue
  156.     # Setup unicode/non-unicode mode
  157.     if [ "$save_only" != yes ]; then
  158.         if [ "$CHARMAP" = UTF-8 ] || [ -z "$ACM$CHARMAP" ]; then
  159.         /bin/echo -n -e '\033%G' >$console
  160.         else
  161.         /bin/echo -n -e '\033%@' >$console
  162.         fi
  163.     fi
  164.  
  165.     # Load the font
  166.     if [ ! -f "$FONT" ]; then
  167.         for dir in \
  168.         /etc/console-setup \
  169.         /usr/local/share/consolefonts \
  170.         /usr/share/consolefonts
  171.         do
  172.           if [ -f "$dir/$CODESET-$FONTFACE$FONTSIZE.psf.gz" ]; then
  173.           FONT="$dir/$CODESET-$FONTFACE$FONTSIZE.psf.gz"
  174.           case "$FONTSIZE" in
  175.               *x*)
  176.               bigfont=yes
  177.               ;;
  178.               *)
  179.               bigfont=no
  180.               ;;
  181.           esac
  182.           break
  183.           fi
  184.         done
  185.     fi
  186.     if [ -f "$FONT" ]; then
  187.         if \
  188.         [ "$save" = yes ] \
  189.         && [ "${FONT%/*}" != /etc/console-setup ]
  190.         then
  191.         cp "$FONT" /etc/console-setup/
  192.         fi
  193.     else
  194.         if which ckbcomp-mini >/dev/null; then
  195.         FONT=$(echo `ls /usr/share/consolefonts/$CODESET-*.psf.gz \
  196.                                  2>/dev/null`)
  197.         FONT=${FONT%% *}
  198.         if [ -z "$FONT" ]; then
  199.             FONT="$CODESET-$FONTFACE$FONTSIZE.psf.gz"
  200.         fi
  201.         else
  202.         FONT="$CODESET-$FONTFACE$FONTSIZE.psf.gz"
  203.         fi
  204.         case "$FONTSIZE" in
  205.         *x*)
  206.             bigfont=yes
  207.             ;;
  208.         *)
  209.             bigfont=no
  210.             ;;
  211.         esac
  212.     fi
  213.     if [ "$save_only" != yes ]; then
  214.         if which consolechars >/dev/null; then
  215.         if [ "$bigfont" = yes ]; then
  216.             echo "setupcon: The consolechars utility from the console-setup font can load only fonts with 8 pixel width matrix.  Please install the setfont utility from the kbd package." >&2
  217.         fi
  218.         eval consolechars -v --tty=$console -f "$FONT" $verbose
  219.         elif which setfont >/dev/null; then
  220.         eval setfont -v -C $console "$FONT" $verbose
  221.         fi
  222.     fi
  223.  
  224.     # Load the ACM
  225.     if [ ! -f "$ACM" ]; then
  226.         for dir in \
  227.         /etc/console-setup \
  228.         /usr/local/share/consoletrans \
  229.         /usr/share/consoletrans
  230.         do
  231.           if [ -f "$dir/$CHARMAP.acm.gz" ]; then
  232.           ACM="$dir/$CHARMAP.acm.gz"
  233.           break
  234.           fi
  235.         done
  236.     fi
  237.     if [ -f "$ACM" ]; then
  238.         if \
  239.         [ "$save" = yes ] \
  240.         && [ "${ACM%/*}" != /etc/console-setup ]
  241.         then
  242.         cp "$ACM" /etc/console-setup/
  243.         fi
  244.     else
  245.         ACM="$CHARMAP.acm.gz"
  246.     fi
  247.     if [ "$save_only" != yes ] && [ "$CHARMAP" != UTF-8 ]; then
  248.         if which consolechars >/dev/null; then
  249.         eval consolechars -v --tty=$console --acm "$ACM" $verbose
  250.         elif which setfont >/dev/null; then
  251.         eval setfont -v -C $console -m "$ACM" $verbose
  252.         fi        
  253.     fi
  254.     done
  255. fi
  256.  
  257. #-----------------------#
  258. #        INPUT          #
  259. #-----------------------#
  260.  
  261. if [ "$font_only" != yes ] && [ "$ACTIVE_CONSOLES" ]; then
  262.     # On Mac PPC machines, we may need to set kernel vars first.  We need
  263.     # to mount /proc to do that, but we need it set up before sulogin may
  264.     # be run in checkroot, which will need the keyboard to log in...
  265.     # This code was borrowed from the keymap.sh script of console-common
  266.     # Copyright ┬⌐ 2001 Yann Dirson
  267.     # Copyright ┬⌐ 2001 Alcove http://www.alcove.fr/
  268.     if [ "$save_only" != yes ] && \
  269.        [ -x /sbin/sysctl ] && [ -r /etc/sysctl.conf ]; then
  270.     if grep -v '^\#' /etc/sysctl.conf | grep -q keycodes ; then
  271.         grep keycodes /etc/sysctl.conf | grep -v "^#" \
  272.         | while read d ; do
  273.               /sbin/sysctl -w $d 2> /dev/null || true
  274.               done
  275.     fi
  276.     fi
  277.     
  278.     if [ "$save_only" != yes ]; then
  279.     for console in $ACTIVE_CONSOLES; do
  280.         [ -w $console ] || continue
  281.         if which kbd_mode >/dev/null; then
  282.         if [ "$CHARMAP" = UTF-8 ] || [ -z "$ACM" ]; then
  283.             kbd_mode -u <$console
  284.         else
  285.             kbd_mode -a <$console
  286.         fi
  287.         fi
  288.     done
  289.     fi
  290.     
  291.     if which loadkeys >/dev/null; then
  292.     if [ "$XKBMODEL" != SKIP ] && which ckbcomp >/dev/null; then
  293.         if [ "$CHARMAP" != UTF-8 ]; then
  294.         acm_option="-charmap $CHARMAP"
  295.         else
  296.         acm_option=''
  297.         fi
  298.  
  299.         if [ "$XKBRULES" ]; then
  300.         rules_option="-rules $XKBRULES"
  301.         else
  302.         rules_option=''
  303.         fi
  304.  
  305.         if [ "$save_only" != yes ]; then
  306.         if \
  307.             [ "$VARIANT" = '' ] \
  308.             && [ -f /etc/console-setup/boottime.kmap.gz ] \
  309.             && [ ! /etc/console-setup/boottime.kmap.gz \
  310.                    -ot /etc/default/console-setup ]
  311.         then
  312.             eval loadkeys /etc/console-setup/boottime.kmap.gz $verbose
  313.         else
  314.             ckbcomp $acm_option $rules_option -model "$XKBMODEL" \
  315.             "$XKBLAYOUT" "$XKBVARIANT" "$XKBOPTIONS" \
  316.             | eval loadkeys $verbose
  317.         fi
  318.         fi
  319.         if which gzip >/dev/null && [ "$save" = yes ]; then
  320.         ckbcomp $acm_option $rules_option -model "$XKBMODEL" \
  321.             "$XKBLAYOUT" "$XKBVARIANT" "$XKBOPTIONS" \
  322.             | gzip -9 >/etc/console-setup/boottime.kmap.gz
  323.         fi
  324.     elif [ "$save_only" != yes ] && \
  325.          [ -f /etc/console-setup/boottime.kmap.gz ]; then
  326.         eval loadkeys /etc/console-setup/boottime.kmap.gz $verbose
  327.     fi
  328.     fi
  329. fi
  330.  
  331.